package cn.edu.eight.food.controller;


import cn.edu.eight.food.model.OfUser;
import cn.edu.eight.food.service.ofUserService;
import cn.edu.eight.food.util.Result;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class ofUserController {
    @Autowired
    ofUserService ofUserService;

    //后台管理中查询用户列表
    @RequestMapping("/msg")
    @ResponseBody
    public Result list(String userId, String username, String mobile) {
        List<OfUser> ofUserList = ofUserService.users(userId,username,mobile);
        Result<OfUser> ofUserResult = new Result<>();
        if (ofUserList != null && ofUserList.size() > 0 ){
            ofUserResult.setCode("0");
            ofUserResult.setMsg("请求成功");
            ofUserResult.setCount(ofUserList.size());
            ofUserResult.setData(ofUserList);
        }else {
            ofUserResult.setCode("500");
            ofUserResult.setMsg("没有数据");
        }
        return ofUserResult;
    }

    //修改用户的账号状态
    @RequestMapping("/save")
    @ResponseBody
    public Result save(@RequestParam String action,
                     @RequestBody OfUser ofUser){
        Result result=new Result();
        if (action.equals("disable")){
            ofUser.setOfUserStatus(3);
            ofUserService.updateUser(ofUser);
            result.setCode("200");
            return result;
        }
        if (action.equals("enable")){
            ofUser.setOfUserStatus(1);
            ofUserService.updateUser(ofUser);
            result.setCode("200");
            return result;
        }
        result.setCode("500");
        return result;
    }

    //删除用户
    @RequestMapping("/del")
    @ResponseBody
    public Result delUser(@RequestBody String[] ids){
        Result result = new Result();
        for (int i=0;i<ids.length;i++){
            ofUserService.deleteUser(ids[i]);
        }
        result.setCode("200");
        return result;
    }

    //添加用户
    @RequestMapping("/addUser")
    @ResponseBody
    public Result addUser(@RequestBody OfUser ofUser){
        Result result = new Result();
        try{
            ofUser.setOfUserStatus(1);
            ofUserService.insert(ofUser);
        }catch (Exception e){
            result.setCode("500");
            return result;
        }
        result.setCode("200");
        return result;
    }

    //注册用户
    @RequestMapping("/register")
    public String registerUser(OfUser ofUser ){
        try{
            ofUser.setOfUserStatus(1);
            ofUserService.insert(ofUser);
        }catch (Exception e){
            Map<String, String> errors = new HashMap<String, String>();
            errors.put("userId", "用户名字已经存在");
        }
        return "redirect:/login/toUserLogin";
    }

    //修改个人信息
    @RequestMapping("/modify")
    public String modifyUser(HttpSession session,OfUser ofUser){
        OfUser user=(OfUser) session.getAttribute("loginUser");
        ofUser.setOfUserId(user.getOfUserId());
        ofUser.setOfUserStatus(user.getOfUserStatus());
        ofUser.setOfUserPw(user.getOfUserPw());
        ofUserService.updateUser(ofUser);
        session.setAttribute("loginUser",ofUser);
        return "redirect:/index/personal";
    }

    @RequestMapping("/modifypsw")
    @ResponseBody
    public Result modifypswUser(HttpSession session,@RequestBody String psw){
        Result result = new Result();
        JSONObject jsonObject = JSONObject.parseObject(psw);
        OfUser user=(OfUser) session.getAttribute("loginUser");
        if (jsonObject.getString("oldpsw").equals(user.getOfUserPw())){
            user.setOfUserPw(jsonObject.getString("newpsw"));
            ofUserService.updateUser(user);
            session.setAttribute("loginUser",user);
            result.setCode("200");
            result.setMsg("修改成功！");
        }else {
            result.setCode("500");
            result.setMsg("原密码错误！");
        }
        return result;
    }
}
